package com.hkfn.rfb.common.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.hkfn.rfb.common.activity.BaseActivity;

import java.io.File;

public class GildeUtils {
    public static void loadImage(Context context, ImageView imageView, String imageUrl){
        if (StringUtils.isEmpty(imageUrl)){
            return;
        }
        if (context instanceof BaseActivity && ((BaseActivity)context).isFinishing()){
            return;
        }
        /// 判断是否是链接 否则base64
        if (!imageUrl.startsWith("http")){
            Glide.with(context).load(AndroidFileUtil.base64ToBitmap(imageUrl)).into(imageView);
        }else {
            Glide.with(context).load(imageUrl).into(imageView);
        }
    }

    public static void loadImage(Context context, final ImageView imageView, String imageUrl, final int errorImage){
        if (StringUtils.isEmpty(imageUrl)){
            return;
        }
        if (context instanceof BaseActivity && ((BaseActivity)context).isFinishing()){
            return;
        }
        /// 判断是否是链接 否则base64
        if (!imageUrl.startsWith("http")){
            Glide.with(context).load(AndroidFileUtil.base64ToBitmap(imageUrl)).into(imageView);
        }else {
            Glide.with(context).load(imageUrl).listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    imageView.setImageResource(errorImage);
                    return true;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    return false;
                }
            }).into(imageView);
        }
    }

    public static void loadImage(Context context, ImageView imageView, int imageUrl){
        if (context instanceof BaseActivity && ((BaseActivity)context).isFinishing()){
            return;
        }
        Glide.with(context).load(imageUrl).into(imageView);
    }

    public static void loadImage(Context context, ImageView imageView, File imageUrl){
        if (null == imageUrl){
            return;
        }
        if (context instanceof BaseActivity && ((BaseActivity)context).isFinishing()){
            return;
        }
        Glide.with(context).load(imageUrl).into(imageView);
    }

    public static void loadImageAsGif(Context context, ImageView imageView, int imageUrl){
        if (context instanceof BaseActivity && ((BaseActivity)context).isFinishing()){
            return;
        }
        Glide.with(context).asGif().load(imageUrl).into(imageView);
    }

    public static void loadImageCorner(Context context, ImageView imageView, String url){
        if (context instanceof BaseActivity && ((BaseActivity)context).isFinishing()){
            return;
        }
        Glide.with(context)
                .load(url)
                .apply(RequestOptions.bitmapTransform(new CornerTransform(context, 15)))
                .into(imageView);
    }

    public static void loadImageCircle(Context context, ImageView imageView, String url){
        if (context instanceof BaseActivity && ((BaseActivity)context).isFinishing()){
            return;
        }
        Glide.with(context)
                .load(url)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(imageView);
    }
}